package cz.muni.fi.pb138.videoweb2013.servlets;

import cz.muni.fi.pb138.videoweb2013.backend.Medium;
import cz.muni.fi.pb138.videoweb2013.backend.MediumManager;
import cz.muni.fi.pb138.videoweb2013.backend.MediumManagerImpl;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

/**
 * search action
 * 
 * @author Filip
 */
@UrlBinding("/action/search.jsp")
public class SearchActionBean implements ActionBean{
    private ActionBeanContext context;
    private String searchedText;
    private static boolean sign = true;
    private static List<Medium> mediums = new ArrayList<Medium>();
    
    public ActionBeanContext getContext() { return context; }
    public void setContext(ActionBeanContext context) { this.context = context; }

    public String getSearchedText() { return searchedText; }
    public void setSearchedText(String searchedText) { this.searchedText = searchedText; }

    public boolean getSign() { return sign; }
    public void setSign(boolean sign) { this.sign = sign; }
    
    public List<Medium> getMediums() { return mediums; }
    public void setMediums(List<Medium> mediums) { this.mediums = mediums; }
    
    @DefaultHandler
    public Resolution search() {
        mediums.clear();
        sign = true;
        allMediums();
        return new ForwardResolution("/search.jsp");
    }
    
    /**
     * finds all mediums with text given by user
     */
    public void allMediums(){
        MediumManager mm = new MediumManagerImpl();
        
        if(mm.getMediumByString(searchedText)!=null){
            mediums.addAll(mm.getMediumByString(searchedText));
            sign = false;
        }
    }
}
